package com.example.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.daoman.Cost;
import com.example.daoman.User;
import com.example.mapper.CostMapper;
import com.example.service.CostService;
import com.example.utils.GetSession;
import com.example.utils.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class CostServiceImpl extends ServiceImpl<CostMapper, Cost> implements CostService {

    //  根据自己所在的楼层和房间查询自己寝室的缴费记录

    @Autowired
    private CostMapper costMapper;

    @Override
    public Message getMyCost(int rid, int fid) {
        Map<String,Object> map = new HashMap<>();
        map.put("roomId",rid);
        map.put("floorId",fid);
        List<Cost> costList = costMapper.getCostList(map);
        return Message.out(200,"查询",costList);
    }

    //    学员缴费

    @Override
    public Message postCost(List<Cost> costList) {
        ArrayList<Integer> arrayList = new ArrayList<>();
//        获取数据id
        for (Cost c:costList){
            arrayList.add(c.getId());
        }
        if (GetSession.get() == null){
            return null;
        }
        Map<String,Object> map = new HashMap<>();
        map.put("statue","已缴费");
        map.put("idList",arrayList);
        map.put("uid", GetSession.get().getId());
        boolean b = costMapper.postCost(map);
        return Message.out(200,"数据更新",b);
    }

//    缴费情况查询
    @Override
    public Message getMyCostTitle() {
        try {
            User user = GetSession.get();
            Map<String,Object> map = new HashMap<>();
            map.put("userId",user.getId());
            List<Cost> costList = costMapper.getCostList(map);
            return Message.out(200,"查询成功",costList);
        }catch (Exception e){
            return Message.out(555,"未查询到用户信息","未查询到用户信息");
        }
    }
}
